package com.app.lxx.pregnancyberth.base;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.app.lxx.pregnancyberth.Mvp.Presenter.BasePresenter;
import com.app.lxx.pregnancyberth.Mvp.View.BaseView;
import com.app.lxx.pregnancyberth.R;
import com.app.lxx.pregnancyberth.widge.SharedPreferencesUtils;
import com.app.lxx.pregnancyberth.widge.UtilsManage;
import com.jaeger.library.StatusBarUtil;


/**
 * Created by Lxx on 2018/4/9.
 * details：BaseActivity
 */

public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements BaseView {
    private P p;
    protected Context context = this;
    protected SharedPreferencesUtils shareference;
    protected UrlManage urlManage;
    protected UtilsManage utilsManage;
    protected LocalBroadcastUtils localBroadcastUtils;
    private RelativeLayout rela_topbar;
    private ImageView iv_back;
    private TextView tv_title;

    public P getP() {
        return p;
    }

    protected abstract P createP();

    private View rootView;

    public View getrootview() {
        return rootView;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        ActivityManger.addac(this);
        p = createP();
        if (p != null) {
            p.attchView(this);
        }
        //   StatusBarUtil.setColor(this, Color.parseColor("#ffffff"), 0);

        setContentView(R.layout.ac_base);
        View decorView = getWindow().getDecorView();
        ViewGroup content = decorView.findViewById(android.R.id.content);
        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.iv_backly).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        LinearLayout linearLayout = (LinearLayout) content.getChildAt(0);
        rootView = getLayoutInflater().inflate(setxmlview(), linearLayout, false);
        linearLayout.addView(rootView);

        rela_topbar = findViewById(R.id.rela_topbar);
        if (hidetitle()) {
            rela_topbar.setVisibility(View.GONE);
        }
        tv_title = findViewById(R.id.tv_title);
        if (TextUtils.isEmpty(settitle())) {
            tv_title.setText("");
        } else {
            tv_title.setText(settitle());
        }


        shareference = new SharedPreferencesUtils(context);
        urlManage = new UrlManage();
        urlManage.APP_URL=UrlManage.APP_URL;
        urlManage.imageUrl=UrlManage.imageUrl;
        utilsManage = new UtilsManage(context);
        localBroadcastUtils = new LocalBroadcastUtils(context);

        setStatusBarColor("#EBEAEA");
        initBindViewId();
    }

    protected abstract void initBindViewId();

    protected abstract boolean hidetitle();

    protected abstract String settitle();

    public void setTopLeftBackImage(int image) {
        iv_back.setImageResource(image);
    }

    public void setTopTitleTvColor(int color) {
        tv_title.setTextColor(getResources().getColor(color));
    }

    public void setTopBackGroundColor(int color) {
        rela_topbar.setBackgroundResource(color);
    }


    @NonNull
    public abstract int setxmlview();

    @Override
    protected void onDestroy() {
        super.onDestroy();

        Log.e("sssssButtonService", "BaseActivity unSubscribe onDestroy");

        ActivityManger.removeac(this);
        if (p != null) {
            p.dettchView();
        }

        //根据 Tag 取消请求
        // OkGo.getInstance().cancelTag(this);
    }

    Toast toast;

    @Override
    public void showToast(String msg) {
        if (toast == null) {
            toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        }
        toast.setText(msg);
        toast.show();
    }

    public void setStatusBarColor(String s) {
        StatusBarUtil.setColor(this, Color.parseColor(s), 0);
    }

}
